﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Web.Media.SmoothStreaming;

using System.Windows.Threading;

namespace LiveStream
{
	public class NativePlayer : SmoothStreamingMediaElement
	{
		const double REFRESH_INTERVAL = 250d;

		DispatcherTimer _refreshTimer;
		TimeSpan _previousEndPosition;
		TimeSpan _previousPosition;

		public EventHandler<EventArgs<TimeSpan>> PositionChanged;
		public EventHandler<EventArgs<TimeSpan>> EndPositionChanged;

		public NativePlayer()
		{
			//   this.DefaultStyleKey = typeof(NativePlayer);
			// Player = new SmoothStreamingMediaElement();
			_refreshTimer = new DispatcherTimer();
			_refreshTimer.Interval = TimeSpan.FromMilliseconds(REFRESH_INTERVAL);
			_refreshTimer.Tick += (s, e) =>
			{
				if (_previousPosition != Position)
				{
					_previousPosition = Position;
					if (PositionChanged != null)
						PositionChanged(this, new EventArgs<TimeSpan>(Position));
				}


				if (_previousEndPosition != EndPosition)
				{
					_previousEndPosition = EndPosition;
					if (EndPositionChanged != null)
						EndPositionChanged(this, new EventArgs<TimeSpan>(EndPosition));
				}
			};
			_refreshTimer.Start();

			this.MediaOpened += new RoutedEventHandler(NativePlayer_MediaOpened);
			this.CurrentStateChanged += new RoutedEventHandler(NativePlayer_CurrentStateChanged);

			//Window parent = Window.GetWindow(this);
			//parent.Closing += new EventHandler<System.ComponentModel.ClosingEventArgs>(Window_Closing);

		}

		void Window_Closing(object sender, System.ComponentModel.ClosingEventArgs e)
		{
			
		}

		//public void WireUp()
		//{
		//    Timeline.ValueChanged += new RoutedPropertyChangedEventHandler<double>(Timeline_ValueChanged);
		////	this.ClipProgressUpdate += new EventHandler<ClipPlaybackEventArgs>(NativePlayer_ClipProgressUpdate);
		//}

		//void NativePlayer_ClipProgressUpdate(object sender, ClipPlaybackEventArgs e)
		//{
		//    MessageBox.Show("Progress");
		//}

		void NativePlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
		{
			//		MessageBox.Show((sender as NativePlayer).CurrentState.ToString());
			//var currentState = (sender as NativePlayer).CurrentState;

			//switch (currentState)
			//{
			//    case SmoothStreamingMediaElementState.Playing:
			//        _refreshTimer.Start();
			//        break;
			//    case SmoothStreamingMediaElementState.Paused:
			//    case SmoothStreamingMediaElementState.Stopped:
			//        _refreshTimer.Start();
			//        break;
			//    default:
			//        break;
			//}
		}

		void NativePlayer_MediaOpened(object sender, RoutedEventArgs e)
		{
			Position = StartPosition;
			//_previousEndPosition = EndPosition;
		}

		//public Button PlayButton { get; set; }
		//public Button StopButton { get; set; }
		//public Timeline Timeline { get; set; }
		//public SmoothStreamingMediaElement Player { get; private set; }

		//public void Timeline_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		//{
		//    if (Timeline.IsScrubbing)
		//    {
		//        this.Position = new TimeSpan(0, 0, 0, 0, (int)e.NewValue);
		//    }
		//}


	}
}
